/**
 * Created by cyf on 15-12-1.
 * 接收消息中心处理类
 */

(function() {
	qxchat.EventHandler = EventHandler;
	var EXTEND = null; //是否需要继承
	function EventHandler() {
		var self = this;
		this.handle = function(data) {
			var dataObj = eval("(" + data + ")");
			//事件
			if (dataObj.type == "event") {
				var d = dataObj.data;
				switch (d.type) {
					//连接打开后登录
					case "SessionOpen":
						//登录
						global.webSocketHandler.login($('#password').val());
						break;
						//登录
					case "Login":
						if (d.status) {
							//登录成功后拉取联系人数据
							global.webSocketHandler.getFirends();
							global.currentUserName = d.user.nickname
							$("#userName_view").html(global.currentUserName)
						} else {
							//登录失败尚未处理
							alert(d.errorMsg)
						}
						break;
						//联系人
					case "Firends":
						var orgTreeData = d.firends.orgTreeData;
						var recentContactsData = d.firends.recentContactsData;
						var personalBandTreeData = d.firends.personalBandTreeData;
						var groupTreeData = d.firends.groupTreeData;
						$('#qx-mainBody').css("display", "inline");
						$('#qx-loginBody').css("display", "none");
						//构建界面
						global.mainIndex.init();
						//创建全体人员树
						global.orgTreeObj = new qxchat.OrgTree("orgTreeView", orgTreeData);
						//创建最近联系人员树
						global.recentContactsDataTreeObj = new qxchat.RecentContactsTree("recentContactsDataView", recentContactsData);
						//创建个人分组人员树
						global.personalBandDataTreeObj = new qxchat.PersonalBandTree("personalBandDataView", personalBandTreeData);
						//创建群组树
						global.groupDataTreeObj = new qxchat.GroupTree("groupTreeView", groupTreeData);
						layer.closeAll('loading');
						break;
						//联系人
					case "Status":
						var _lName = d.loginName;
						var _lStatus = d.status;
						var g_orgTreeObj_treeNode = global.orgTreeObj.zTreeObj.getNodeByParam("loginName", _lName);
						var g_orgTreeObj_ustatus = $("#" + g_orgTreeObj_treeNode.tId + "_span_status");
						switch (_lStatus) {
							case 0:
								g_orgTreeObj_ustatus.css("color", "gray");
								g_orgTreeObj_ustatus.html("离线");
								break;
							case 1:
								g_orgTreeObj_ustatus.css("color", "green");
								g_orgTreeObj_ustatus.html("在线");
								break;
						}
						var g_recentContactsDataTreeObj_treeNode = global.recentContactsDataTreeObj.zTreeObj.getNodeByParam("loginName", _lName);
						if (g_recentContactsDataTreeObj_treeNode != null) {
							var g_recentContactsDataTreeObj_ustatus = $("#" + g_recentContactsDataTreeObj_treeNode.tId + "_span_status");
							switch (_lStatus) {
								case 0:
									g_recentContactsDataTreeObj_ustatus.css("color", "gray");
									g_recentContactsDataTreeObj_ustatus.html("离线");
									break;
								case 1:
									g_recentContactsDataTreeObj_ustatus.css("color", "green");
									g_recentContactsDataTreeObj_ustatus.html("在线");
									break;
							}
						}
						var g_personalBandDataTreeObj_treeNode = global.personalBandDataTreeObj.zTreeObj.getNodeByParam("loginName", _lName);
						if (g_personalBandDataTreeObj_treeNode != null) {
							var g_personalBandDataTreeObj_ustatus = $("#" + g_personalBandDataTreeObj_treeNode.tId + "_span_status");
							switch (_lStatus) {
								case 0:
									g_personalBandDataTreeObj_ustatus.css("color", "gray");
									g_personalBandDataTreeObj_ustatus.html("离线");
									break;
								case 1:
									g_personalBandDataTreeObj_ustatus.css("color", "green");
									g_personalBandDataTreeObj_ustatus.html("在线");
									break;
							}
						}
						break;
				}
				//消息
			} else if (dataObj.type == "msg") {
				var d = dataObj.data;
				var _cwin = null;
				var i = 0;
				for (i = 0; i < global.chatWinArray.length; i++) {
					if (global.chatWinArray[i].loginName == d.from) {
						//找到对应窗口
						_cwin = global.chatWinArray[i].win;
						break;
					}
				}
				//如果已打开聊天窗口
				if (_cwin != null) {
					try {
						//写消息
						_cwin.window.window.receiveLog(d);
						_cwin.focus();
					} catch (e) {
						//窗口无效则清楚集合内对象
						global.chatWinArray.splice(i, 1);
						//提醒后创建窗口并打开
						var nwNotify = global.nwNotify;
						nwNotify.setConfig({
							appIcon: nwNotify.getAppPath() + 'qx.png',
							displayTime: 6000
						});
						nwNotify.notify({
							title: "来自：" + d.fromName + "的消息",
							text: $.base64.decode(d.msg),
							onClickFunc: function(event) {
								var _chatwin = global.chartWinHandler.open(d.from, d.fromName);
								event.closeNotification();
							}
						});
					}
				} else {
					//提醒后创建窗口并打开
					var nwNotify = global.nwNotify;
					nwNotify.setConfig({
						appIcon: nwNotify.getAppPath() + 'qx.png',
						displayTime: 6000
					});
					nwNotify.notify({
						title: "来自：" + d.fromName + "的消息",
						text: $.base64.decode(d.msg),
						onClickFunc: function(event) {
							var _chatwin = global.chartWinHandler.open(d.from, d.fromName);
							event.closeNotification();
						}
					});
				}
				//群消息
			} else if (dataObj.type == "groupMsg") {
				var d = dataObj.data;
				var _cwin = null;
				var i = 0;
				for (i = 0; i < global.groupChatWinArray.length; i++) {
					if (global.groupChatWinArray[i].groupId == d.groupId) {
						//找到对应窗口
						_cwin = global.groupChatWinArray[i].win;
						break;
					}
				}
				//如果已打开聊天窗口
				if (_cwin != null) {
					try {
						//写消息
						_cwin.window.window.receiveLog(d);
					} catch (e) {
						//窗口无效则清楚集合内对象
						global.groupChatWinArray.splice(i, 1);
						//提醒后创建窗口并打开
						var nwNotify = global.nwNotify;
						nwNotify.setConfig({
							appIcon: nwNotify.getAppPath() + 'qx.png',
							displayTime: 6000
						});
						nwNotify.notify({
							title: "来自：" + d.groupName + "的消息",
							text: $.base64.decode(d.msg),
							onClickFunc: function(event) {
								var _chatwin = global.groupChatWinHandler.open(d.groupId, d.groupName);
								event.closeNotification();
							}
						});
					}
				} else {
					//提醒后创建窗口并打开
					var nwNotify = global.nwNotify;
					nwNotify.setConfig({
						appIcon: nwNotify.getAppPath() + 'qx.png',
						displayTime: 6000
					});
					nwNotify.notify({
						title: "来自：" + d.groupName + "的消息",
						text: $.base64.decode(d.msg),
						onClickFunc: function(event) {
							var _chatwin = global.groupChatWinHandler.open(d.groupId, d.groupName);
							event.closeNotification();
						}
					});
				}
			}
		}
	}
})();